<?php

class String {

	/**
	 * 产生随机字符串
	 *
	 * @param int $length
	 * @param string $string
	 * @return string
	 */
	public static function random($length, $string = '23456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz') {
		$rstr = '';
		$strlen = strlen($string);
		for ($i=0; $i<$length; $i++) {
			$rstr .= $string{mt_rand(0,$strlen-1)};	
		}
		return $rstr;
	}

	/**
	 * 字符串截取
	 *
	 * @param string $str
	 * @param int $len
	 * @param string $dot
	 * @param string $encoding
	 * @return string
	 */
	public static function cut($str, $len = 0, $dot = '...', $encoding = 'utf-8') {
		if (!$len || strlen($str) <= $len) return $str;
		$tempstr = '';
		$str = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $str);
		if ($encoding == 'utf-8') {
			$n = $tn = $noc = 0;
			while($n < strlen($str)) {
				$t = ord($str[$n]);
				if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1; $n++; $noc++;
				} elseif (194 <= $t && $t <= 223) {
					$tn = 2; $n += 2; $noc += 2;
				} elseif (224 <= $t && $t < 239) {
					$tn = 3; $n += 3; $noc += 2;
				} elseif (240 <= $t && $t <= 247) {
					$tn = 4; $n += 4; $noc += 2;
				} elseif (248 <= $t && $t <= 251) {
					$tn = 5; $n += 5; $noc += 2;
				} elseif ($t == 252 || $t == 253) {
					$tn = 6; $n += 6; $noc += 2;
				} else {
					$n++;
				}
				if($noc >= $len) {
					break;
				}
			}
			if($noc > $len) {
				$n -= $tn;
			}
			$tempstr = substr($str, 0, $n);
		} elseif ($encoding == 'gbk') {
			for ($i=0; $i<$len; $i++) {
				$tempstr .= ord($str{$i}) > 127 ? $str{$i}.$str{++$i} : $str{$i};
			}
		}
		$tempstr = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $tempstr);
		return $tempstr.$dot;
	}

	/**
	 * 字符串过滤
	 *
	 * @param string $string
	 * @param boolean $isurl
	 * @return string
	 */
	public static function safeStr($string, $isurl = false) {
		$string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string);
		$string = str_replace(array("\0","%00","\r"),'',$string);
		empty($isurl) && $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&amp;',$string);
		$string = str_replace(array("%3C",'<'),'&lt;',$string);
		$string = str_replace(array("%3E",'>'),'&gt;',$string);
		$string = str_replace(array('"',"'","\t",'  '),array('&quot;','&#39;','    ','&nbsp;&nbsp;'),$string);
		return trim($string);
	}
	
	/**
	 * 数组元素串接
	 *
	 * 将数组的每个元素用逗号连接起来，并给每个元素添加单引号，用于SQL语句中串接ID
	 * @param array $array
	 * @return string
	 */
	public static function simplode($array) {
		return "'".implode("','", $array)."'";
	}
	
	/**
	 * 为特殊字符加上反斜杠
	 * 
	 * 与addslashes不同之处在于本函数支持数组
	 * @param string|array $string
	 * @return string|array 返回处理后的变量
	 */
	public static function addSlashes($string) {
		if (!is_array($string)) return addslashes($string);
		foreach ($string as $key => $val) {
			$string[$key] = self::addSlashes($val);
		}
		return $string;
	}
	
	/**
	 * 去除特殊字符的反斜杠
	 * 
	 * 与stripslashes不同之处在于本函数支持数组
	 * @param string|array $string
	 * @return string|array 返回处理后的变量
	 */
	function delSlashes($string) {
		if (!is_array($string)) return stripslashes($string);
		foreach ($string as $key => $val) {
			$string[$key] = self::delSlashes($val);
		}
		return $string;
	}
}
